import { add, subtract, multiply, divide } from 'lodash-es'
import { isNumber } from '@/common/utils/types'
import Decimal from 'decimal.js'
export const simpleOperation = {
	toDecimalComputeNum: (res) => {
		if (isNumber(res)) {
			return Number(res.toFixed(2))
		} else {
			return res
		}
	},
	toAdd: function (...args) {
		const res = add(...args)
		return this.toDecimalComputeNum(res)
	},
	toSubtract: function (...args) {
		const res = subtract(...args)
		return this.toDecimalComputeNum(res)
	},
	toMultiply: function (...args) {
		const res = multiply(...args)
		return this.toDecimalComputeNum(res)
	},
	toDivide: function (...args) {
		const res = divide(...args)
		return this.toDecimalComputeNum(res)
	}
}
export const intricateOperation = {
	toDecimalComputeNum: (res) => {
		if (isNumber(res)) {
			return Number(res.toFixed(2))
		} else {
			return res
		}
	},
	toAdd: (num1, num2) => {
		return new Decimal(num1).add(new Decimal(num2)).toNumber()
	},
	toSubtract: (num1, num2) => {
		return new Decimal(num1).sub(new Decimal(num2)).toNumber()
	},
	toMultiply: (num1, num2) => {
		return new Decimal(num1).mul(new Decimal(num2)).toNumber()
	},
	toDivide: (num1, num2) => {
		if (num2 === 0) {
			throw new Error('除数不能为 0')
		}
		return new Decimal(num1).div(new Decimal(num2)).toNumber()
	}
}
